<template>
  <div class="activity-container">
    <!-- 标题和操作按钮 -->
    <div class="header">
      <div class="title">
        <el-icon><Calendar /></el-icon>
        <h2>活动管理</h2>
      </div>
      <el-button type="primary" @click="handleAdd">
        <el-icon><Plus /></el-icon>新增活动
      </el-button>
    </div>

    <!-- 搜索栏 -->
    <div class="search-container">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <div class="form-item-group">
          <el-form-item label="活动名称">
            <el-input
                v-model="searchForm.activityName"
                placeholder="请输入活动名称"
                clearable
                prefix-icon="Search"
            />
          </el-form-item>
          <el-form-item label="活动状态">
            <el-select
                v-model="searchForm.status"
                placeholder="请选择状态"
                clearable
                style="width: 160px"
            >
              <el-option label="进行中" value="进行中" />
              <el-option label="已结束" value="已结束" />
              <el-option label="注销" value="注销" />
            </el-select>
          </el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>查询
          </el-button>
        </div>
        <div class="button-group">
          <el-button @click="resetSearch">
            <el-icon><Refresh /></el-icon>重置
          </el-button>
        </div>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <div class="table-container">
      <el-table :data="tableData" border stripe>
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="activityName" label="活动名称" />
        <el-table-column prop="activityType" label="活动类型" />
        <el-table-column prop="activityPeriod" label="活动周期" />
        <el-table-column prop="applicableScope" label="适用范围" />
        <el-table-column prop="status" label="活动状态" />
        <el-table-column prop="creationTime" label="创建时间" :formatter="formatDate" />
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleView(row)">
              <el-icon><View /></el-icon>查看
            </el-button>
            <el-button type="primary" link @click="handleEdit(row)">
              <el-icon><Edit /></el-icon>编辑
            </el-button>
            <el-button type="danger" link @click="handleDelete(row)">
              <el-icon><Delete /></el-icon>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

    <!-- 详情/编辑对话框 -->
    <ActivityDialog
        v-model="dialogVisible"
        :mode="dialogMode"
        :activity-data="currentActivity"
        @refresh="fetchData"
    />
  </div>
</template>

<script setup>
    import { ref, onMounted } from 'vue'
    import { ElMessage, ElMessageBox } from 'element-plus'
    import ActivityDialog from './components/ActivityDialog.vue'
    import axios from 'axios'
    import {
      Calendar,
      Plus,
      Search,
      Refresh,
      View,
      Edit,
      Delete
    } from '@element-plus/icons-vue'  // 添加图标导入
    // 数据定义
    const searchForm = ref({
        activityName: '',
        status: ''
    })
    const tableData = ref([])
    const currentPage = ref(1)
    const pageSize = ref(10)
    const total = ref(0)
    const dialogVisible = ref(false)
    const dialogMode = ref('view') // view, edit, add
    const currentActivity = ref(null)

    // 获取数据
    const fetchData = async () => {
        try {
            const { data } = await axios.get('/activityInfo/list', {
                params: {
                    current: currentPage.value,
                    size: pageSize.value,
                    activityName: searchForm.value.activityName,
                    status: searchForm.value.status
                }
            })
            tableData.value = data.records
            total.value = data.total
        } catch (error) {
            ElMessage.error('获取数据失败')
        }
    }

    // 事件处理
    const handleSearch = () => {
        currentPage.value = 1
        fetchData()
    }

    const resetSearch = () => {
        searchForm.value = {
            activityName: '',
            status: ''
        }
        handleSearch()
    }

    const handleAdd = () => {
        dialogMode.value = 'add'
        currentActivity.value = null
        dialogVisible.value = true
    }

    const handleView = (row) => {
        dialogMode.value = 'view'
        currentActivity.value = row
        dialogVisible.value = true
    }

    const handleEdit = (row) => {
        dialogMode.value = 'edit'
        currentActivity.value = row
        dialogVisible.value = true
    }

    const handleDelete = (row) => {
        ElMessageBox.confirm('确认删除该活动?', '警告', {
            type: 'warning'
        }).then(async () => {
            try {
                await axios.delete(`/activityInfo/delete?id=${row.id}`)
                ElMessage.success('删除成功')
                fetchData()
            } catch (error) {
                ElMessage.error('删除失败')
            }
        })
    }

    const handleSizeChange = (val) => {
        pageSize.value = val
        fetchData()
    }

    const handleCurrentChange = (val) => {
        currentPage.value = val
        fetchData()
    }

    const formatDate = (row, column, cellValue) => {
        if (!cellValue) return ''
        return new Date(cellValue).toLocaleString()
    }

    onMounted(() => {
        fetchData()
    })
</script>

<style scoped>
/* 容器基础样式 */
.activity-container {
  padding: 24px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 48px);
}

/* 头部样式 */
.header {
  margin-bottom: 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  padding: 16px 24px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.header .title {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header .title h2 {
  margin: 0;
  font-size: 20px;
  color: #303133;
}

/* 搜索区域样式 */
.search-container {
  margin-bottom: 24px;
  background-color: #fff;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.search-form {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.form-item-group {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  flex: 1;
}

.button-group {
  display: flex;
  gap: 8px;
  align-items: center;
  white-space: nowrap;
}

/* 表格区域样式 */
.table-container {
  background-color: #fff;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  margin-bottom: 24px;
}

/* 分页区域样式 */
.pagination-container {
  background-color: #fff;
  padding: 16px 24px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  display: flex;
  justify-content: flex-end;
}

/* 响应式布局 */
@media screen and (max-width: 768px) {
  .search-form {
    flex-direction: column;
    align-items: stretch;
  }

  .form-item-group {
    flex-direction: column;
    width: 100%;
  }

  .button-group {
    width: 100%;
    justify-content: flex-end;
    margin-top: 16px;
  }

  :deep(.el-form-item) {
    margin-right: 0;
    width: 100%;
  }
}

/* 通用样式调整 */
:deep(.el-form-item) {
  margin-bottom: 0;
  margin-right: 0;
}

:deep(.el-form-item__content) {
  flex-wrap: nowrap;
}
</style>